/**
 * 
 */
package cn.edu.pku.sei.ra.selfadapt.actions;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


import cn.edu.pku.sei.ra.framework.CausalConnector;
import cn.edu.pku.sei.ra.selfadapt.OCLAnnoExec;
import cn.edu.pku.sei.ra.ui.CausalConnectorRunnerAction;

/**
 * @author songhui
 *
 */
public class OCLManagementAgentCreationDialog extends Dialog {
	
	Combo workingProject=null;
	Combo arcMetaURI=null;
	Combo arcFile=null;
	Combo ccCombo=null;
	Text interval=null;
	
	IWorkspaceRoot root=null;
	IProject project=null;
	OCLAnnoExec kernel=null;
	CausalConnector cc=null;
	

	/**
	 * @param parentShell
	 */
	public OCLManagementAgentCreationDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param parentShell
	 */
	public OCLManagementAgentCreationDialog(IShellProvider parentShell) {
		super(parentShell);
		
	}
	
	protected Control createDialogArea(Composite parent) {
		parent=(Composite)super.createDialogArea(parent);
		GridLayout gdlayout=new GridLayout(3,false);
		GridData gddata=null;
		parent.setLayout(gdlayout);
		
		gddata=new GridData(GridData.FILL_VERTICAL);
		gddata.widthHint=300;
		gddata.heightHint=20;
		gddata.horizontalSpan=3;
		
		Label label=null;
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Select a project for environment");
		label.setLayoutData(gddata);
		workingProject=new Combo(parent,SWT.LEFT);		
		workingProject.setLayoutData(gddata);
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Select the registered URI for architecture meta model");
		label.setLayoutData(gddata);
		arcMetaURI=new Combo(parent,SWT.LEFT);
		arcMetaURI.setLayoutData(gddata);
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Select the architecture file");
		label.setLayoutData(gddata);
		arcFile=new Combo(parent,SWT.LEFT);
		arcFile.setLayoutData(gddata);
		
				
		label=new Label(parent,SWT.LEFT);
		label.setText("Select an Existing Causal Connector");
		label.setLayoutData(gddata);
		ccCombo=new Combo(parent,SWT.LEFT);
		ccCombo.setLayoutData(gddata);
		
		
		GridData gdsmall=new GridData(GridData.FILL_VERTICAL);
		gdsmall.heightHint=20;
		gdsmall.widthHint=80;
		gdsmall.horizontalSpan=1;
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Interval");
		label.setLayoutData(gdsmall);
		
		interval=new Text(parent,SWT.LEFT);
		interval.setLayoutData(gdsmall);
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Second(s)");
		label.setLayoutData(gdsmall);
		
		
		workingProject.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				initFiles();
			}
		});
		
		root=ResourcesPlugin.getWorkspace().getRoot();
		initWorkingProject();
		initPackage();
		initCCCombo();

		return parent;
	}
	
	private void initFiles(){
		String prj=workingProject.getText();
		project=root.getProject(prj);
		//project.getFolder("").get
		File[] files=project.getLocation().toFile().listFiles();
		for(int i=0;i<files.length;i++){
			String fn=files[i].getName();
			if(fn.endsWith(".qvt")){
				;
			}
			else if(fn.endsWith(".ecore")){
				;
			}
			else{
				arcFile.add(fn);
			}
		}
		
	}
	
	private void initCCCombo(){
		for(CausalConnector cc:CausalConnectorRunnerAction.ALL_CAUSAL_CONNECTORS){
			ccCombo.add(cc.toString());
		}
	}
	
	private void initPackage(){
		Set<String> keys=EPackage.Registry.INSTANCE.keySet();
		ArrayList<String> al=new ArrayList();
		al.addAll(keys);
		Collections.sort(al);
		for(String key:al){
			arcMetaURI.add(key);
		}
		arcMetaURI.select(0);
	}
	
	
	
	private void initWorkingProject(){
		IProject projects[]=root.getProjects();
		for(int i=0;i<projects.length;i++){
			String loc=projects[i].getName();
			workingProject.add(loc);			
		}
		workingProject.select(0);
		initFiles();
	}
	private String fileName=null;
	private int intInterval=1;
	protected void okPressed(){
		String prj=project.getLocation().toPortableString();
		String modelPath=prj+"/"+arcFile.getText();
		EPackage pack=EPackage.Registry.INSTANCE.getEPackage(arcMetaURI.getText());
		kernel=new OCLAnnoExec(pack,modelPath);
		String ccName=ccCombo.getText();
		for(CausalConnector c:CausalConnectorRunnerAction.ALL_CAUSAL_CONNECTORS){
			if(c.toString().equals(ccName)){
				cc=c;
				break;
			}
		}
		fileName=arcFile.getText();
		intInterval=Integer.valueOf(interval.getText()).intValue();
		super.okPressed();
	}
	
	public OCLAnnoExec getOCLAnnoExec(){
		return kernel;
	}
	
	public CausalConnector getCausalConnector(){
		return cc;
	}
	
	public int getInterval(){
		return intInterval;
	}
	
	public String getDesc(){
		return project.getName()+"/"+fileName;
	}
}
